local DIY = require "packages/diy_utility/diy_utility"

local xunshi = fk.CreateSkill{
  name = "lvshi__xunshi",
  tags = {"Turn"},
}

Fk:loadTranslationTable{
  ["lvshi__xunshi"] = "巡使",
  [":lvshi__xunshi"] = "回合技，当你的体力值变化后或使用锦囊牌后，你可用任意张手牌交换中央区内等量的牌。",
  ["#lvshi__xunshi-invoke"] = "巡使：你可拖拽任意张手牌交换中央区内等量的牌",
  ["#lvshi__xunshi-cards"] = "巡使：你可以选择至多 %arg 张手牌交换中央区内等量的牌",

  ["$lvshi__xunshi1"] = "此间不明我明之，此事不平我平之！",
  ["$lvshi__xunshi2"] = "长车琳、铁甲铮，桓侯既至百冤明！",
  ["$lvshi__xunshi3"] = "鸣鼓净街魑魅退，擂瓮升堂罪何人!",
  ["$lvshi__xunshi4"] = "巡界奔走双甲子，归来两界又一秋！",
}

xunshi:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

local spec = {
  can_trigger = function(self, event, target, player, data)
    if event == fk.CardUseFinished then
      if data.card.type ~= Card.TypeTrick then return false end
    end
    return player:hasSkill(xunshi.name) and target == player and player:usedSkillTimes(xunshi.name) == 0
    and player:getHandcardNum() > 0 and #DIY.getCenterCards(player.room) > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local results = room:askToArrangeCards(player, {
      skill_name = xunshi.name,
      card_map = {
        player.general, player:getCardIds("h"),
        "@$CenterArea", DIY.getCenterCards(room),
      },
      max_limit = {#player:getCardIds("h"), #DIY.getCenterCards(room)},
      prompt = "#lvshi__xunshi-invoke",
      cancelable = true,
    })
    if #results[1] == 0 or #results[2] == 0 then return end
    local cards1 = table.filter(results[1], function (id)
      return room:getCardArea(id) == Card.DiscardPile
    end)
    local cards2 = table.filter(results[2], function (id)
      return table.contains(player:getCardIds("h"), id)
    end)
    local move1 = {
      ids = cards2,
      from = player.id,
      fromArea = Card.PlayerHand,
      toArea = Card.DiscardPile,
      moveReason = fk.ReasonPutIntoDiscardPile,
      skillName = xunshi.name,
    }
    local move2 = {
      ids = cards1,
      to = player.id,
      toArea = Card.PlayerHand,
      moveReason = fk.ReasonJustMove,
      skillName = xunshi.name,
    }
    room:moveCards(move1, move2)
  end,
}

xunshi:addEffect(fk.HpChanged, spec)
xunshi:addEffect(fk.CardUseFinished, spec)

return xunshi